package com.nananer7.dao;

import com.nananer7.DBAccess;

import java.sql.*;

public class UserDao {


    //根据用户名查找用户密码
    public String findPswByUsername(String username){
        String pwd = null;
        Connection con =null;
        PreparedStatement pstmt =null;
        ResultSet rs = null;
        try {
            con = DBAccess.getConnection();
            pstmt = con.prepareStatement("select * from user where uname=?");
            pstmt.setString(1, username);//将第一个问号的值设为username
            rs = pstmt.executeQuery();
            if(rs==null){
                return null;
            }
            if(rs.next()){
                pwd=rs.getString("pwd");
            }else{
                pwd=null;
            }
        }catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(pstmt!=null)pstmt.close();
                if(con!=null)con.close();
            }
            catch (SQLException e) {
            }
        }
        return pwd;
    }

    //根据用户名查找用户ID
    public int findUidByUsername(String username){
        int uid = 0;
        Connection con =null;
        PreparedStatement pstmt =null;
        ResultSet rs = null;
        try {
            con = DBAccess.getConnection();
            pstmt = con.prepareStatement("select * from user where uname=?");
            pstmt.setString(1, username);//将第一个问号的值设为username
            rs = pstmt.executeQuery();
            if(rs==null){
                return 0;
            }
            if(rs.next()){
                uid=rs.getInt("uid");
            }else{
                uid=0;
            }
        }catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(pstmt!=null)pstmt.close();
                if(con!=null)con.close();
            }
            catch (SQLException e) {
            }
        }
        return uid;
    }

    //根据用户ID查找用户名
    public String findUsernameByUid(int uid){
        String username =null;
        Connection con =null;
        PreparedStatement pstmt =null;
        ResultSet rs = null;
        try {
            con = DBAccess.getConnection();
            pstmt = con.prepareStatement("select * from user where uid=?");
            pstmt.setInt(1, uid);
            rs = pstmt.executeQuery();
            if(rs==null){
                return null;
            }
            if(rs.next()){
                username=rs.getString("uname");
            }else{
                username=null;
            }
        }catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(pstmt!=null)pstmt.close();
                if(con!=null)con.close();
            }
            catch (SQLException e) {
            }
        }
        return username;
    }

    //添加用户
    public boolean addUser(String username,String psw,String email){
        Connection con =null;
        PreparedStatement pstmt =null;
        try {
            con = DBAccess.getConnection();
            String sql = "INSERT INTO user(uname,pwd,email) VALUES(?,?,?)";
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, username);
            pstmt.setString(2, psw);
            pstmt.setString(3, email);
            if(pstmt.executeUpdate()==1){
                return true;
            }else{
                return false;
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(pstmt!=null)
                    pstmt.close();
                if(con!=null)
                    con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    //单独测试使用
   /* public static void main(String[] args) {
        String psw =new UserDao().findPswByUsername("Nancy");
        System.out.println(psw);
        boolean bl=new UserDao().addUser("anan","12345","571888125@qq.com");
        System.out.println(bl);
    }*/
}
